import sys
from PyQt5.QtWidgets import (
    QWidget,
    QApplication,
    QLabel,
    QPushButton,
    QLineEdit,
    QHBoxLayout,
    QVBoxLayout,
    QComboBox,
    QTextEdit,
)
from PyQt5.QtCore import QCoreApplication, pyqtSignal


class mainWindowUI(QWidget):
    signal_write_msg = pyqtSignal(str)
    def __init__(self, num):
        """
        初始化窗口
        :param num: 计数窗口
        """
        super().__init__()
        self.num = num
        self.init_UI()

    def init_UI(self):
        """
        设置小部件
        """

        self.resize(640, 480)
        self.setWindowTitle(
            QCoreApplication.translate(
                "TCP-UDP", "TCP/UDP网络测试工具-窗口%s" % self.num
            )
        )

        # 定义控件
        self.label_ip = QLabel(self)
        self.lineEdit_local_ip = QLineEdit(self)
        self.pushButton_get_ip = QPushButton(self)

        self.label_port = QLabel(self)
        self.lineEdit_port = QLineEdit(self)
        self.pushButton_else = QPushButton(self)

        self.label_sendto = QLabel(self)
        # 初始状态隐藏部件
        self.label_sendto.hide()
        
        self.lineEdit_ip_send = QLineEdit(self)
        self.lineEdit_ip_send.hide()

        self.comboBox_tcp = QComboBox(self)
        self.pushButton_link = QPushButton(self)
        self.pushButton_unlink = QPushButton(self)

        # 设置按键按下状态
        self.pushButton_unlink.setEnabled(False) 

        self.label_send = QLabel(self)
        self.textEdit_send = QTextEdit(self)

        self.label_written = QLabel(self)
        self.pushButton_send = QPushButton(self)
        self.pushButton_exit = QPushButton(self)
        self.label_recive = QLabel(self)
        self.pushButton_clear = QPushButton(self)
        self.textEdit_recive = QTextEdit(self)

        # 定义布局
        self.h_box_1 = QHBoxLayout()
        self.h_box_2 = QHBoxLayout()
        self.h_box_3 = QHBoxLayout()

        self.h_box_4 = QHBoxLayout()

        self.h_box_exit = QHBoxLayout()

        self.h_box_revice = QHBoxLayout()

        self.v_box_right = QVBoxLayout()

        self.v_box_set = QVBoxLayout()
        self.v_box_send = QVBoxLayout()
        self.v_box_exit = QVBoxLayout()

        self.h_box_all = QHBoxLayout()

        # 向选择功能的下拉菜单添加选项
        self.comboBox_tcp.addItem("")
        self.comboBox_tcp.addItem("")
        self.comboBox_tcp.addItem("")
        self.comboBox_tcp.addItem("")
        self.comboBox_tcp.addItem("")

        self.ui_translate()

        # 设置窗体布局
        self.layout_UI()
        # 设置窗体布局到窗体
        self.setLayout(self.h_box_all)

        self.show()

    def ui_translate(self):
        """
        控件默认显示文字的设置
        :param: QWidgetc类创建的对象
        :return: None
        """
        self.label_ip.setText("本机IP：")
        # self.lineEdit_local_ip.setText()
        self.pushButton_get_ip.setText("重新获取IP")
        self.label_port.setText("端口号：")
        self.pushButton_else.setText("窗口多开")
        self.label_sendto.setText('目标IP：')

        self.comboBox_tcp.setItemText(0, "TCP服务端")
        self.comboBox_tcp.setItemText(1, "TCP客户端")
        self.comboBox_tcp.setItemText(2, "UDP服务端")
        self.comboBox_tcp.setItemText(3, "UDP客户端")
        self.comboBox_tcp.setItemText(4, "WEB服务端")
        self.pushButton_link.setText("连接网络")
        self.pushButton_unlink.setText("断开网络")
        self.label_send.setText("发送区域")

        self.label_written.setText("Written by xiabaoqin")
        self.pushButton_send.setText("发送")
        self.pushButton_exit.setText("退出系统")

        self.label_recive.setText('接受区域')
        self.pushButton_clear.setText('清除区域')
        self.textEdit_recive.setPlainText("这是窗口-%s\n" % self.num)

    def layout_UI(self):
        # 左侧布局添加
        self.h_box_1.addWidget(self.label_ip)
        self.h_box_1.addWidget(self.lineEdit_local_ip)
        self.h_box_1.addWidget(self.pushButton_get_ip)

        self.h_box_2.addWidget(self.label_port)
        self.h_box_2.addWidget(self.lineEdit_port)
        self.h_box_2.addWidget(self.pushButton_else)

        self.h_box_3.addWidget(self.comboBox_tcp)
        self.h_box_3.addWidget(self.pushButton_link)
        self.h_box_3.addWidget(self.pushButton_unlink)
        
        self.h_box_4.addWidget(self.label_sendto)
        self.h_box_4.addWidget(self.lineEdit_ip_send)

        self.v_box_send.addWidget(self.label_send)
        self.v_box_send.addWidget(self.textEdit_send)

        self.v_box_exit.addWidget(self.pushButton_send)
        self.v_box_exit.addWidget(self.pushButton_exit)

        self.h_box_exit.addWidget(self.label_written)
        self.h_box_exit.addLayout(self.v_box_exit)

        self.v_box_set.addLayout(self.h_box_1)
        self.v_box_set.addLayout(self.h_box_2)
        self.v_box_set.addLayout(self.h_box_4)
        self.v_box_set.addLayout(self.h_box_3)
       
        self.v_box_set.addLayout(self.v_box_send)
        self.v_box_set.addLayout(self.h_box_exit)

        # 添加左侧布局
        self.h_box_revice.addWidget(self.label_recive)
        self.h_box_revice.addWidget(self.pushButton_clear)

        self.v_box_right.addLayout(self.h_box_revice)
        self.v_box_right.addWidget(self.textEdit_recive)

        # 将左右侧布局添加到窗体布局
        self.h_box_all.addLayout(self.v_box_set)
        self.h_box_all.addLayout(self.v_box_right)


    def closeEvent(self, event):
        '''
        重写closeEvent方法，实现QWidget窗体关闭时执行一些代码
        :param event: close()触发的事件
        :return: None
        '''
        self.close_all()

    def close_all(self):
        pass



if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = mainWindowUI(1)
    sys.exit(app.exec_())
